<script lang="ts" setup>
import { ruleDataDeleteAPI, ruleDataGetAPI } from '@/apis/car';
import type { RowRule, RuleDate, RuleDateParams } from '@/types/car';
import { ElMessage } from 'element-plus';
import { onMounted, ref } from 'vue';
import { utils,  writeFileXLSX } from 'xlsx';
const params = ref<RuleDateParams>({
  page: 1,
  pageSize: 10
})
const total = ref(0)
const dialogVisible = ref(false)
const getRuleData=async()=>{
  const res=await ruleDataGetAPI(params.value)
  ruleData.value=res.data
}
const chargeType=new Map<RowRule['chargeType'],string>([['duration','时长收费'],
['partition','分段收费'],['turn','按次收费']])
onMounted(() => {
  getRuleData()
})
const ruleData=ref<RuleDate>({} as RuleDate)
const deleteRuleData=async(id:string)=>{
  await ruleDataDeleteAPI(id)
  getRuleData()
  ElMessage.success('删除成功')
}
const exportExcel=()=>{
  const workbook=utils.book_new()
  const worksheet=utils.json_to_sheet(ruleData.value.rows.map((item,index)=>({...item,id:index+1})))
  utils.book_append_sheet(workbook,worksheet,'Data')
  utils.sheet_add_aoa(worksheet,[['姓名','年龄']],{ origin: 'A1' })
  writeFileXLSX(workbook,'测试.xlsx')
}
</script>

<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary">增加停车计费规则</el-button>
      <el-button @click="exportExcel">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleData.rows" style="width: 100%">
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上限(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式">
          <template #default="{row}">
            {{ chargeType.get(row.chargeType) }}
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="{row}">
            <el-button size="small" type="text">编辑</el-button>
            <el-button size="small" type="text" @click="deleteRuleData(row.id)" >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.rules .el-form-item {
  padding: 0 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>